import dayjs from "dayjs";

export default function (source) {
  let output = {
    labels: new Array(),
    values: new Array(),
    values1: new Array(),
    values2: new Array(),
    summary: 0,
    summary1: 0,
    summary2: 0,
    count: 0,
  };
  let users = [];
  if (source && source.content) {
    let start = dayjs(source.start);
    let end = source.end == null ? dayjs() : source.end;
    users = source.users;
    while (start.isSameOrBefore(end)) {
      let sectors = new Array();
      let sectors1 = new Array();
      let sectors2 = new Array();
      source.content.forEach((item) => {
        if (
          start.isSameOrAfter(item.startTime) &&
          start.isSameOrBefore(item.endTime == null ? dayjs() : item.endTime) &&
          sectors.indexOf(item.seatCode) == -1 &&
          ["PLANNER", "OMA01", "SUP01", "SUP02"].indexOf(item.seatCode) == -1
        ) {
          sectors.push(item.seatCode);
          let room = matchRoom(item.seatCode, item.sectorCodes.split(","));
          // let office = matchOfficeByUserID(item.userId, users);
          if (room === 2) {
            // office == "管制二室"
            sectors2.push(item.seatCode);
          } else if (room === 1) {
            // office == "管制一室"
            sectors1.push(item.seatCode);
          }
        }
      });
      if (sectors.length > 0) {
        output.values.push(sectors.length);
        output.values1.push(sectors1.length);
        output.values2.push(sectors2.length);
        output.labels.push(start.format("MM/DD\nHH:mm"));
        output.summary += sectors.length * 0.5;
        output.summary1 += sectors1.length * 0.5;
        output.summary2 += sectors2.length * 0.5;
        output.count++;
      }
      start = start.add(30, "minute");
    }
  }
  return output;
}

const matchOfficeByUserID = (userID, users) => {
  // {"name": "陈华宽", "number": 20070023, "office": "管制一室"}
  for (let i = 0; i < users.length; i++) {
    if (userID == users[i].number) {
      return users[i].office;
    }
  }
};

const sectorCodeRoom1 = [
  "AP01",
  "AP02",
  "AP11",
  "AP81",
  "APA1",
  "AP18",
  "AP1A",
];
const sectorCodeRoom2 = [
  "AP12",
  "APL7",
  "APR7",
  "APL9",
  "APR9",
  "AP9L",
  "AP9R",
  "AP7L",
  "AP7R",
  "AP03",
  "AP04",
  "AP5L",
];
const seatCodeRoom1 = [
  "TM14",
  "TM13",
  "TM10",
  "TM08",
  "TM01",
  "TM02",
  "TM11",
  "ZUTFNTZ",
];

const matchRoom = (seatCode, sectorCodes) => {
  let room = null;
  for (let i = 0; i < sectorCodes.length; i++) {
    let isRoom1 = sectorCodeRoom1.includes(sectorCodes[i]);
    let isRoom2 = sectorCodeRoom2.includes(sectorCodes[i]);
    if (isRoom1) {
      if (room === null || room === 1) {
        room = 1;
      } else {
        room = null;
        break;
      }
    } else if (isRoom2) {
      if (room === null || room === 2) {
        room = 2;
      } else {
        room = null;
        break;
      }
    }
  }
  if (room !== null) {
    return room;
  } else if (seatCodeRoom1.includes(seatCode)) {
    return 1;
  } else {
    return 2;
  }
};
